<?php

namespace app\api\controller;
use app\common\controller\Api;
use app\common\model\Coupons as CouponsModel;
use app\common\model\UserCoupons;
use app\common\model\CouponsOrder;

class Coupons extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    public function info(){
        $user_id = $this->auth->id;
        $data['item'] = (new CouponsModel)->where(['id'=>1])->find();
        $data['total_count'] = (new UserCoupons)->where([
            'user_id' => $user_id,
        ])->sum('buyed');

        $data['remain_count'] = (new UserCoupons)->where([
            'user_id' => $user_id,
        ])->sum('nums');

        $data['use_count'] = $data['total_count'] - $data['remain_count'];

        $this->success('success', $data);
    }

    public function add_order()
    {
        $params = $this->request->post();
        $order = CouponsOrder::addOrder($params);
        $this->success('订单添加成功', $order);
    }

    //我的券
    public function my()
    {
        $user_id = $this->auth->id;
        $data = (new UserCoupons)->with('coupons')->where([
            'user_id' => $user_id,
        ])->paginate(10);
        $this->success('success', $data);
    }

}